package communication;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import main.Declarations;

import Data.HibernateUtil;

import executor.CreateFileInDB;
import executor.DeleteFileInDB;
import executor.ExecutorsHash;
import executor.RenameFileInDB;
/**
 * responsible for get the data from the service - the last
 * changes in the server
 * 
 * @author Vladimir Sarkisyan & Erez Levy
 */
public class ProtocolRunable implements Runnable{
	BufferedReader in; PrintWriter out;
	
	
	 public ProtocolRunable(BufferedReader in, PrintWriter out){
		  this.in=in; this.out=out;
		 }
	 
	 /**
	  * take requests from service - messages about last changes in server
	  * 
	  * take requests from the service, in this messages the data about
	  * last changes in server, call to the treatTheRequest that check these 
	  * strings and check in the DB, if he is updated to this changes in the server
	  * 
	  */
	@Override
	public void run() {
		String updated;
		
		// TODO Auto-generated method stub
		  String request="",response="";
		  try {

		  while(true){
			  try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			  //System.out.println("ProtocolRunable while");
			   request=in.readLine();
			   //System.out.println(request);
			   if(request!=null){
				   System.out.println(request);
				   updated = treatTheRequest(request);
				   //send if the file updated
				   out.println(updated);
			   }
			   /*response=hm.get(request);
			    * 
			   if(response!=null) {
				   out.println(response);
				   System.out.println(response);
			   }
			   */
			   
			  
		   }//
		   } catch (IOException e) {}
	}
	//end run
	
	
	
	/**
	 * treat the Request and check if DB is updated
	 * 
	 * checking if the file created, deleted, renamed, create 
	 * and after that check if the file is exist/deleted/renamed in the DB
	 * 
	 * @param request - came from the service
	 */
	public String treatTheRequest(String request){
		System.out.println("in ProtocolRunable - treatTheRequest");
		System.out.println("request:  " + request);
		String []arr;
		arr = request.split(" ");
		//System.out.println(arr[0]);
		
		//check if the file exist
		if (arr[0].equals("created")){
			System.out.println("ProtocolRunable - in create");
			if(HibernateUtil.instance.fileExist(arr[1])){
				System.out.println(" ProtocolRunable - updated - fileName "+arr[1]);
				return "true";
			}
			else{
				System.out.println(" ProtocolRunable - not updated - fileName "+arr[1]);
				return "false";
			}
		}
		
		//if the file dont exist
		else if(arr[0].equals("deleted")){
			if(HibernateUtil.instance.fileExist(arr[1])){
				
				//delete the file
				//File tmp = new File("");
		        String path=Declarations.instance.getPath();
		        System.out.println("path: "+path);
		        
		        path+="\\";
		        path+=arr[1];
		        File f = new File(path);
		        f.delete();
		        
		        return "false";
			}
			else{
				return "true";
			}
		}
		
		//check if the new file exist
		else if(arr[0].equals("renamed")){
	   		System.out.println("in rename");
	   		//set the new name of the file
	   		//for debug 
	   		System.out.println("NewFileName - "+ arr[2]);
	   		//
			if(HibernateUtil.instance.fileExist(arr[2])){
				return "true";
			}
			else{
		        
				String path=Declarations.instance.getPath();
		        System.out.println("path: "+path);
		        
		        path+="\\";
		        path+=arr[1];
		        File f = new File(path);
		        //rename the file
		        f.renameTo(new File(arr[2]));

				return "false";
			}
   		
		}
		return "false";	
	}
	//end of treatTheRequest
}
